import EventBus from '@/event-bus/EventBus.js';
import { generateRules, genRequireRule } from '@/utils/rules';

export default {
  data() {
    return {
      rules: {
        dataSourceType: generateRules({
          label: this.$demoi18n('panel.label.dataSourceType'),
        }),
        dataSource: generateRules({
          label: this.$demoi18n('panel.label.selectDataSource'),
        }),
        apiCode: generateRules({
          label: this.$demoi18n('panel.label.selectApi'),
        }),
        kafkaTopic: generateRules({
          label: this.$demoi18n('panel.label.fafkaKey'),
        }),
        dataPackage: generateRules({
          label: this.$demoi18n('panelData.label.xtagDataPackage'),
        }),
        dataTable: generateRules({
          label: this.$demoi18n('panelData.label.xtagDataTable'),
        }),

        rangeFrom: generateRules({
          label: this.$demoi18n('panel.label.rangeSource'),
        }),
        type: generateRules({
          label: this.$demoi18n('panel.label.type'),
        }),
        rangeDataType: generateRules({
          label: this.$demoi18n('word.valueType'),
        }),
        timeType: generateRules({
          label: this.$demoi18n('word.timeFormat'),
        }),
      },
    };
  },
  methods: {
    generateRules,
    genRequireRule,
    check() {
      return new Promise(resolve => {
        this.$refs.form.validate((valid, obj) => {
          let limitValid = true;
          if (this.checkLimit) {
            limitValid = this.checkLimit();
          }
          const res = limitValid && valid;
          if (!res) {
            if (this.curComp) {
              this.curComp = 'table';
            }
            resolve(false);
          }

          // 左表右图处理
          if (!this.$refs.form_chart) {
            resolve(true);
            return;
          }

          this.$refs.form_chart.validate(validChart => {
            if (!validChart) {
              if (this.curComp) {
                this.curComp = this.$options.name.startsWith('Gantt') ? 'gantt' : 'shape';
              }
              resolve(false);
            }
            resolve(true);
          });
        });
      });
    },

    // 兼容旧组件的操作，对数据页签中explain的括号做转换处理,
    parenthesesConversion() {
      // 来自prop
      const tableData = this.dataForm && this.dataForm.tableData;
      if (tableData && tableData.length) {
        tableData.forEach(item => {
          if (item.explain) {
            item.explain = item.explain.replace(/\(/g, '（');
            item.explain = item.explain.replace(/\)/g, '）');
          }
        });
      }

      // 来自this.config
      const tableDataConfig = this.config?.config?.data?.tableData;
      if (tableDataConfig && tableDataConfig.length) {
        tableDataConfig.forEach(item => {
          if (item.explain) {
            item.explain = item.explain.replace(/\(/g, '（');
            item.explain = item.explain.replace(/\)/g, '）');
          }
        });
      }
    },
  },
  created() {
    this.parenthesesConversion();
    EventBus.$on('vis-panel-data-form-clearValidate', () => {
      this.$refs.form?.clearValidate();
    });
  },
  unmounted() {
    EventBus.$off('vis-panel-data-form-clearValidate');
  },
};
